<?php

namespace nimo\domains\acc\validator;

use File;
use Illuminate\Support\MessageBag;
use Illuminate\Validation\Validator;
use nimo\domains\acc\models\Book;

class BooksValidator extends Validator {
    protected static $_resources = null;
    protected $datas = null;
    protected $configs = null;
    public function __construct($datas, array $configs, array $messages = array(), array $customAttributes = array()) {
        static::$_resources = File::getRequire(__DIR__ . '/BooksValidatorConfig.php');
        $messages = $messages + static::$_resources['messages'];
        $customAttributes = $customAttributes + static::$_resources['customAttributes'];
        parent::__construct(app('translator'), array(), static::$_resources['validators'], $messages, $customAttributes);
        $this->datas = $datas;
        $this->configs = $configs;
    }
    protected function getAttribute($attribute) {
        return "(" . ($this->index + 1) . ":$attribute)";
    }
    protected function doReplacements($message, $attribute, $rule, $parameters) {
        $message = parent::doReplacements($message, $attribute, $rule, $parameters);
        if (isset($this->data[$attribute])) {
            return "$message\n" . $this->data[$attribute];
        }
        return "$message\n" . json_encode($parameters, JSON_UNESCAPED_UNICODE);
    }
    protected function calculateTax($type, $amount, $tax, $percent, &$tax_amount) {
        // 税額
        $ret = false;
        switch ($type) {
            case 0:
                // 小数点以下の四捨五入
                $amount_without_tax = intval(round($amount/(1 + doubleval($percent)/100)));
                $tax_amount = ($amount -  $amount_without_tax);
                $ret = (intval($tax) == $tax_amount);
                break;
            case 1:
                // 小数点以下の切捨て
                $tax_amount = intval($calc_tax_amount);
                $ret = (intval($tax) == $tax_amount);
                break;
            case 2:
                // 小数点以下の切上げ
                break;
            default:
                break;
        }
        return $ret;
    }
    protected function validateRow() {
        // 借方消費税額
        if (! empty($this->data['d_tax_pct'])) {
            //($total,$tax,$percent,$tax_amount) = list($this->data['d_amount'],$this->data['d_amount_tax'],$this->data['d_tax_pct'], null);
            $amount = $this->data['d_amount'];
            $tax = $this->data['d_amount_tax'];
            $percent = $this->data['d_tax_pct'];
            $calc_kbn = $this->data['d_tax_calc_kbn'];
            $calc_tax = null;
            if (!$this->calculateTax(0, $amount, $tax, $percent, $calc_tax)) {
                $this->addError('d_amount_tax', 'same', array($calc_tax));
            }
        }
        // 貸方消費税額
        if (! empty($this->data['c_tax_pct'])) {
            $amount = $this->data['c_amount'];
            $tax = $this->data['c_amount_tax'];
            $percent = $this->data['c_tax_pct'];
            $calc_kbn = $this->data['c_tax_calc_kbn'];
            $calc_tax = null;
            if (!$this->calculateTax(0, $amount, $tax, $percent, $calc_tax, $calc_kbn)) {
                $this->addError('c_amount_tax', 'same', array($calc_tax));
            }
        }
        // _amount 金額
        // _amount_tax 消費税額
        // _tax_pct 税率
        // _tax_calc_kbn 税計算区分
        
        // _tax_kbn 税区分
        // _tax_pct_kbn 積上計算区分
    }
    public function passesRow($index, array $data) {
        $this->data = $data;
        $this->index = $index;
        $count = count($this->messages->all());
        // 対象年月
        $this->validate('ym', 'in:' . $this->configs['ym']);
        foreach ( $this->rules as $attribute => $rules ) {
            foreach ( $rules as $rule ) {
                $this->validate($attribute, $rule);
            }
        }
        // その他
        $this->validateRow();
        return (count($this->messages->all()) - $count) === 0;
    }
    public function passes() {
        $this->messages = new MessageBag();
        $amount = array('c_amount' => 0,'d_amount' => 0,'d_amount_tax' => 0,'c_amount_tax' => 0);
        foreach ( $this->datas as $i => $v ) {
            $this->passesRow($i, $v);
            foreach ( array_keys($amount) as $attribute ) {
                $amount[$attribute] += intval($this->getValue($attribute));
            }
        }
        if ($amount['c_amount'] != $amount['d_amount']) {
            $this->addError('sum_balance', 'sum_balance', $amount);
        }
        // dd(json_encode($amount, JSON_UNESCAPED_UNICODE));
        return count($this->messages->all()) === 0;
    }
}
